package com.xm2.demo.system.shop.service;

import com.xm2.demo.model.Center;
import com.xm2.demo.model.Form;
import com.xm2.demo.model.Stores;
import com.xm2.demo.system.center.dao.CentersRepository;
import com.xm2.demo.system.order.dao.OrderRepository;
import com.xm2.demo.system.shop.dao.StoresRepository;
import com.xm2.demo.system.shop.dto.StoreInfo;
import com.xm2.demo.system.shop.dto.StoresDto;
import com.xm2.demo.system.shop.dto.StoresPage;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Create
 * @create 2018-12-13 11:44
 * @Description：
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private StoresRepository storesRepository;
    @Autowired
    private CentersRepository centersRepository;
    @Autowired
    private OrderRepository orderRepository;

    @Override
    public void addStore(StoreInfo storeInfo) {
        Stores stores = new Stores();
        BeanUtils.copyProperties(storeInfo, stores);
        Center center = centersRepository.findCenterByCenterName(storeInfo.getCenterName());
        center.getStores().add(stores);
        stores.setCenter(center);
        storesRepository.save(stores);
    }

    @Override
    public Pagination findByPageable(Pagination pagination) {
        pagination.setTotalRows(storesRepository.findAll().size());
        Pageable pageable = new PageRequest(pagination.getCurrentPage(), pagination.getSize(), Sort.Direction.ASC, "id");
        pagination.setPage(storesRepository.findAll(pageable));
        return pagination;
    }


    @Override
    public void deleteByIds(String ids) {
        // 接收包含stuId的字符串，并将它分割成字符串数组
        String[] stuList = ids.split(",");
        // 将字符串数组转为List<Intger> 类型
        List<Integer> LString = new ArrayList<Integer>();
        for(String str : stuList){
            LString.add(new Integer(str));
        }
        storesRepository.deleteByIds(LString);
    }

    @Override
    public void update(StoresDto storesDto) {
        Stores stores = new Stores();
        BeanUtils.copyProperties(storesDto,stores);
        Center center = centersRepository.findCenterByCenterName(storesDto.getCenterName());
        stores.setCenter(center);
        storesRepository.save(stores);
    }

    @Override
    public Pagination findByStoresPage(StoresPage storesPage) {
        storesPage.setTotalRows(storesRepository.findStoresByStoresName(storesPage.getStoresName()).size());
        List<Stores> storesList = storesRepository.findStoresByStoresNamePage(storesPage.getStoresName(),storesPage.getStartRow(),storesPage.getSize());
        storesPage.setList(storesList);
        return storesPage;
    }

    @Override
    public List<Stores> getShop(Integer id) {
        Form form = orderRepository.findOne(id);
        Center center = centersRepository.findCenterByCenterName(form.getCenter());
        List<Stores> storesList = storesRepository.findStoresByCenterId(center.getId());
        return storesList;
    }

    @Override
    public void bind(Integer id, String shop) {
        Form form = orderRepository.findOne(id);
        form.setShop(shop);
        orderRepository.save(form);
    }
}
